<template>
  <div class="add-form">
    <el-page-header @back="$router.back()" :content="`${form.id ? '编辑':'添加'}`">
    </el-page-header>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px" style="margin-top: 20px;">
      <el-form-item label="上级菜单">
        <ParentMenu v-model="form.parent_id"></ParentMenu>
      </el-form-item>
      <el-form-item label="菜单名称" prop='menu_name'>
        <el-input v-model="form.menu_name"></el-input>
      </el-form-item>
      <el-form-item label="路由" prop="route">
        <el-input v-model="form.route"></el-input>
      </el-form-item>
      <el-form-item label="路由别名" prop="code">
        <el-input v-model="form.code"></el-input>
      </el-form-item>
      <el-form-item label="组件路劲" prop="component">
        <el-input v-model="form.component"></el-input>
      </el-form-item>
      <el-form-item label="icon">
        <el-input v-model="form.icon"></el-input>
      </el-form-item>
      <el-form-item label="是否菜单">
        <el-radio v-model="form.is_menu" label="1">是</el-radio>
        <el-radio v-model="form.is_menu" label="0">否</el-radio>
      </el-form-item>
      <el-form-item label="排序">
        <el-input-number v-model="form.sort" label="描述文字"></el-input-number>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">提交</el-button>
      </el-form-item>

    </el-form>

  </div>
</template>

<script>
  import ParentMenu from './components/ParentMenu.vue'
  import {
    ajaxPost
  } from '@/api/ajax'
  export default {
    components: {
      ParentMenu,
    },
    data() {
      return {
        form: {
          id: '',
          menu_name: '',
          route: '',
          icon: '',
          sort: 0,
          parent_id: '', //上级菜单
          is_menu: '0',
          code: '',
          component:'',
        },
        rules: {
          menu_name: [{
            required: true,
            message: '请输入菜单名称',
            trigger: 'blur'
          }, ],
          route: [{
            required: true,
            message: '请输入菜单路由',
            trigger: 'blur'
          }, ],
          code: [{
            required: true,
            message: '请输入路由别名',
            trigger: 'blur'
          }],
          component: [{
            required: true,
            message: '请输入路由别名',
            trigger: 'blur'
          }]
        },
        row: [],
      }
    },
    created() {
      if (this.$route.params.id) {
        this.form = this.$route.params;
        this.form.parent_id = this.form.parent_id.toString();
        this.form.is_menu = this.form.is_menu.toString();
      }
    },
    methods: {
      onSubmit() {
        this.$refs.form.validate((valid) => {
          if (valid) {
            let data = { ...this.form
            };
            if (data.parent_id == '') {
              data.parent_id = 0;
            }
            if (data.id) {
              ajaxPost('menu/update', data).then(res => {
                if (res.code == 0) {
                  this.$message({
                    message: '编辑成功',
                    type: "success",
                    duration: 1500,
                    onClose: () => {
                      this.$router.back();
                    },
                  })
                }
              })
            } else {
              ajaxPost('menu/create', data).then(res => {
                if (res.code == 0) {
                  this.$message({
                    message: '添加成功',
                    type: "success",
                    duration: 1500,
                    onClose: () => {
                      this.$router.back();
                    },
                  })
                }
              });
            }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      }
    }
  }
</script>

<style lang="scss">
  .add-form {
    padding: 20px;
    box-sizing: border-box;
    /* input {
      width: 500px;
    } */
  }
</style>
